/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.supplier.adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.AgentMyApplyRecord;
import com.epfresh.supplier.entity.BaseStoreListBean;
import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class AgentMakeBaseTitleViewBinder
    extends ItemViewBinder<BaseStoreListBean, AgentMakeBaseTitleViewBinder.ViewHolder> implements View.OnClickListener {
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }
    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
        @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_make_agent_title, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_status);
        if (onRecyclerViewItemClickListener != null && o instanceof String) {
            onRecyclerViewItemClickListener.onItemClick(v, 0, o);
        }
    }
    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull BaseStoreListBean category) {
        holder.tv_make_agent_title.setText(category.getName());
        holder.tv_make_agent_title.setTag(R.id.item_key_status, category.getId());
        holder.tv_make_agent_title.setOnClickListener(this);
        holder.tv_make_agent_go.setTag(R.id.item_key_status, category.getId());
        holder.tv_make_agent_go.setOnClickListener(this);
        holder.tv_make_agent_detail.setTag(R.id.item_key_status, category.getId());
        holder.tv_make_agent_detail.setOnClickListener(this);
        if(category.getAgentState()!=null&&category.getAgentState().equals("2")){
            holder.tv_make_agent_detail.setVisibility(View.VISIBLE);
            Drawable nav_up = holder.tv_make_agent_title.getContext().getResources().getDrawable(R.mipmap.buyer_agent);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());

            holder.tv_make_agent_title.setCompoundDrawables(nav_up,null,null,null);
        }else {
            holder.tv_make_agent_detail.setVisibility(View.GONE);
            holder.tv_make_agent_title.setCompoundDrawables(null ,null,null,null);
        }

        if (category.getAgentState() == null) {
            holder.tv_make_agent_go.setText("申请代办");
            holder.tv_make_agent_go.setTextColor(Color.parseColor("#ff7900"));
            GradientDrawable gd = (GradientDrawable)holder.tv_make_agent_go.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.parseColor("#ff7900"));
        } else if (category.getAgentState().equals("1")) {
            holder.tv_make_agent_go.setText("申请中");
            holder.tv_make_agent_go.setTextColor(Color.parseColor("#666666"));
            GradientDrawable gd = (GradientDrawable)holder.tv_make_agent_go.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.parseColor("#666666"));
        } else if (category.getAgentState().equals("2")) {
            holder.tv_make_agent_go.setText("代办中");
            holder.tv_make_agent_go.setTextColor(Color.parseColor("#666666"));
            GradientDrawable gd = (GradientDrawable)holder.tv_make_agent_go.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.parseColor("#666666"));
        } else if (category.getAgentState().equals("3")) {
            holder.tv_make_agent_go.setText("重新申请");
            holder.tv_make_agent_go.setTextColor(Color.parseColor("#ff7900"));
            GradientDrawable gd = (GradientDrawable)holder.tv_make_agent_go.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.parseColor("#ff7900"));
        } else if (category.getAgentState().equals("5")) {
            holder.tv_make_agent_go.setText("解除中");
            holder.tv_make_agent_go.setTextColor(Color.parseColor("#666666"));
            GradientDrawable gd = (GradientDrawable)holder.tv_make_agent_go.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.parseColor("#666666"));
        } else if (category.getAgentState().equals("4")) {
            holder.tv_make_agent_go.setText("暂停代办");
            holder.tv_make_agent_go.setTextColor(Color.parseColor("#666666"));
            GradientDrawable gd = (GradientDrawable)holder.tv_make_agent_go.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.parseColor("#666666"));
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

         private final TextView tv_make_agent_title;
         private final TextView tv_make_agent_go;
         private final TextView tv_make_agent_detail;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_make_agent_title = itemView.findViewById(R.id.tv_make_agent_title);
            tv_make_agent_go = itemView.findViewById(R.id.tv_make_agent_go);
            tv_make_agent_detail = itemView.findViewById(R.id.tv_make_agent_detail);
        }
    }
}
